package com.zyg.model;

import com.zyg.common.ResultCode;

import java.io.Serializable;

/**
 * 通用返回对象
 * @param <T>
 */
public class Result<T> implements Serializable {

    private int code;

    private String msg;

    private T data;// 数据

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result initSuccess() {
        Result result = new Result();
        result.code = ResultCode.SUCCESS.getCode();
        result.msg = ResultCode.SUCCESS.getMsg();
        return result;
    }

    public Result initSuccess(T data) {
        Result result = new Result();
        result.code = ResultCode.SUCCESS.getCode();
        result.msg = ResultCode.SUCCESS.getMsg();
        result.data = data;
        return result;
    }

    public Result initError() {
        Result result = new Result();
        result.code = ResultCode.ERROR.getCode();
        result.msg = ResultCode.ERROR.getMsg();
        return result;
    }

    public Result initError(String msg) {
        Result result = new Result();
        result.code = ResultCode.ERROR.getCode();
        result.msg = msg;
        return result;
    }

    public Result initCodeAndMsg(ResultCode resultCode) {
        Result result = new Result();
        result.code = resultCode.getCode();
        result.msg = resultCode.getMsg();
        return result;
    }

    public Result initCodeAndMsgAndData(ResultCode resultCode,T data) {
        Result result = new Result();
        result.code = resultCode.getCode();
        result.msg = resultCode.getMsg();
        result.data = data;
        return result;
    }
}
